package morenoapp.components.input;

import java.awt.BorderLayout;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import morenoapp.MorenoApp;
import morenoapp.components.common.SwingAppPanel;
import morenoapp.components.common.table.column.MorenoCustomTableColumnModel;
import morenoapp.components.common.table.renderer.ProxyPanelTableCellRenderer;
import morenoapp.components.common.table.renderer.RespondentProxyPanelTableCellRenderer;
import morenoapp.components.help.MorenoHelp;
import morenoapp.model.data.MorenoModelFace;
import morenoapp.model.data.MorenoRespondentFace;
import morenoapp.model.enums.MorenoRespondentsSortOrder;
import morenoapp.util.EnumWrapper;
import morenoapp.util.MorenoCollections;
import morenoapp.util.MorenoSwing;
import org.jdesktop.application.Action;

/**
 *
 * @author dmitry.mamonov
 */
public class InputRespondentsPanel extends SwingAppPanel {
    protected MorenoModelFace model = MorenoApp.getModel();
    protected Set<MorenoRespondentSelecetionListener> morenoRespondentSelecetionListeners = new HashSet<MorenoRespondentSelecetionListener>();
    protected JToolBar toolbar;
    protected JButton buttonAddRespondent;
    protected JButton buttonImportRespondents;
    protected JButton buttonRemoveRespondents;
    protected JComboBox comboDoSortRespondets;
    protected JScrollPane scrollRespondentsTable;
    protected JTable tableRespondents;
    protected InputRespondentsTableModel tableModelRespondents;
    /**
     * 
     */
    public InputRespondentsPanel() {
        initComponents();
        initLayout();
    }

    @Override
    protected void initComponents() {
        setName(getClass().getSimpleName());
        {
            toolbar = new JToolBar();
            toolbar.setFloatable(false);
        }
        {
            JButton button = new JButton(getActions().get("actionAddRespondent"));
            button.setName("buttonAddRespondent");
            buttonAddRespondent = button;
        }
        {
            JButton button = new JButton(getActions().get("actionImportRespondents"));
            button.setName("buttonImportRespondents");
            buttonImportRespondents = button;
        }
        {
            JButton button = new JButton(getActions().get("actionRemoveRespondents"));
            button.setName("buttonRemoveRespondents");
            buttonRemoveRespondents = button;
        }
        {
            JComboBox combo = new JComboBox(EnumWrapper.createWrappersNullAndAll(MorenoRespondentsSortOrder.class));
            combo.setName("comboDoSortRespondets");
            combo.setAction(getActions().get("actionDoSortRespondents"));
            comboDoSortRespondets = combo;
        }
        {
            JScrollPane scroll = new JScrollPane();
            scroll.setName("scrollRespondentsTable");
            scrollRespondentsTable = scroll;
        }
        {
            tableModelRespondents = new InputRespondentsTableModel();
        }
        {
            JTable table = new JTable();
            table.setName("tableRespondents");
            table.setColumnModel(new MorenoCustomTableColumnModel(tableModelRespondents));
            table.setModel(tableModelRespondents);
            int index = -1;
            for(TableColumn tc:MorenoCollections.toList(table.getColumnModel().getColumns())){
                index++;
                Class<?> columnClass  = tableModelRespondents.getColumnClass(index);
                TableCellRenderer baseRenderer;
                baseRenderer= tc.getCellRenderer();
                if (baseRenderer==null) {
                    baseRenderer = table.getDefaultRenderer(columnClass);
                }
                System.out.println("RENDER: "+baseRenderer);
                //baseRenderer = table.getDefaultRenderer(columnClass);
                if ((baseRenderer instanceof ProxyPanelTableCellRenderer)==false){
                    TableCellRenderer wrappedRenderer = new RespondentProxyPanelTableCellRenderer(baseRenderer);
                    tc.setCellRenderer(wrappedRenderer);
                }
            }
            
            table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent e) {
                    if (!e.getValueIsAdjusting()) {
                        if (tableRespondents.getSelectedRowCount() == 1) {
                            fireMorenoRespondentSeleceted(MorenoApp.getModel().getRespondentsList().get(tableRespondents.getSelectedRow()));
                        } else {
                            fireMorenoRespondentSeleceted(null);
                        }
                    }
                }

            });

            tableRespondents = table;
        }

    }

    @Override
    protected void initLayout() {
        setLayout(new BorderLayout());
        {
            toolbar.add(buttonAddRespondent);
            toolbar.add(buttonImportRespondents);
            toolbar.add(buttonRemoveRespondents);
            toolbar.add(comboDoSortRespondets);
            toolbar.add(MorenoHelp.createHelpButton("input_respondents_list"));
        }
        add(toolbar, BorderLayout.NORTH);
        {
            scrollRespondentsTable.setViewportView(tableRespondents);
        }
        add(scrollRespondentsTable, BorderLayout.CENTER);
    }

    public void addMorenoRespondentSelecetionListener(MorenoRespondentSelecetionListener lst) {
        morenoRespondentSelecetionListeners.add(lst);
    }

    public void removeMorenoRespondentSelecetionListener(MorenoRespondentSelecetionListener lst) {
        morenoRespondentSelecetionListeners.remove(lst);
    }

    protected void fireMorenoRespondentSeleceted(MorenoRespondentFace aRespondent) {
        for (MorenoRespondentSelecetionListener lst : morenoRespondentSelecetionListeners) {
            lst.onRespondentSelected(aRespondent);
        }
    }

    @Action
    public void actionAddRespondent() {
        model.createRespondent();
    }

    @Action
    public void actionImportRespondents() {
        ImportRespondentsListJDialog importDialog = new ImportRespondentsListJDialog(MorenoApp.getApplication().getMainFrame(), true);
        importDialog.setVisible(true);
    }

    @Action
    public void actionRemoveRespondents() {
        List<MorenoRespondentFace> respondents = new ArrayList<MorenoRespondentFace>();
        for (int row : tableRespondents.getSelectedRows()) {
            respondents.add(tableModelRespondents.getRespondentAtRow(row));
        }
        model.deleteRespondents(respondents);
    }

    @Action
    public void actionDoSortRespondents() {
        EnumWrapper<MorenoRespondentsSortOrder> orderWrapper = (EnumWrapper<MorenoRespondentsSortOrder>) comboDoSortRespondets.getSelectedItem();
        if (orderWrapper.getWrappedValue()!=null){
            model.sortRespondents(orderWrapper.getWrappedValue());
        }
        comboDoSortRespondets.setSelectedItem(new EnumWrapper<MorenoRespondentsSortOrder>(MorenoRespondentsSortOrder.class, null));
    }

    public static void main(String[] args) throws IOException {
        MorenoSwing.showComponentInFrame(new InputRespondentsPanel());
        MorenoSwing.generateActionsProperties(InputRespondentsPanel.class, new InputRespondentsPanel().getActions());
    }

}
